/*
 * main.cpp
 *
 *  Created on: 24 nov 2011
 *      Author: Robert
 */

#include <iostream>
#include "utils/EventDispatcher.h"
#include "Simulator.h"


class Activity : public EventDispatcher
{

};

class Observer
{
	public:
		Observer() : eventOneHasOccured(false), eventTwoHasOccured(false) {}

		void handleEventOneA(const Event &event) {
			eventOneHasOccured = true;
			std::cout << "Event one (a)" << std::endl;
		}
		void handleEventOneB(const Event &event) {
			eventOneHasOccured = true;
			std::cout << "Event one (b)" << std::endl;
		}
		void handleEventTwo(const Event &event) {
			eventTwoHasOccured = true;
			std::cout << "Event two" << std::endl;
		}

	private:
		bool eventOneHasOccured;
		bool eventTwoHasOccured;
};


/*
 * template<typename T>
EventListenerT<typename T> ET(T &object, T::MemberFunction function)
{
	return EventListenerT<typename T>(object, function);
}
*/



void testEventDispatcher()
{


	Activity activity;
	Observer observer;


	EventListenerT<Observer> observeEventOneA(observer, &Observer::handleEventOneA);
	EventListenerT<Observer> observeEventOneB(observer, &Observer::handleEventOneB);
	EventListenerT<Observer> observeEventTwo(observer, &Observer::handleEventTwo);

	activity.dispatchEvent(Event("ONE"));
	activity.dispatchEvent(Event("TWO"));
	std::cout << (activity.hasEventListener("ONE") ? "Event ONE exists although it shouldn't" : "Event ONE doesn't exist") << std::endl;
	std::cout << (activity.hasEventListener("TWO") ? "Event TWO exists although it shouldn't" : "Event TWO doesn't exist") << std::endl;

	activity.addEventListener("ONE", observeEventOneA, false, -100);
	activity.addEventListener("ONE", observeEventOneB, false, 100);
	activity.addEventListener("TWO", observeEventTwo);
	std::cout << (activity.hasEventListener("ONE") ? "Event ONE exists" : "Event ONE doesn't exists although it should") << std::endl;

	activity.dispatchEvent(Event("ONE"));
	activity.dispatchEvent(Event("TWO"));

	activity.removeEventListener("ONE", observeEventOneB);
	activity.removeEventListener("ONE", observeEventTwo);
	std::cout << (activity.hasEventListener("ONE") ? "Event ONE exists" : "Event ONE doesn't exists although it should") << std::endl;
	std::cout << (activity.hasEventListener("TWO") ? "Event TWO exists" : "Event TWO doesn't exists although it should") << std::endl;

	activity.dispatchEvent(Event("ONE"));
	activity.dispatchEvent(Event("TWO"));

	activity.removeEventListener("ONE", observeEventOneA);
	activity.removeEventListener("TWO", observeEventTwo);
	std::cout << (activity.hasEventListener("ONE") ? "Event ONE exists although it shouldn't" : "Event ONE doesn't exist") << std::endl;
	std::cout << (activity.hasEventListener("TWO") ? "Event TWO exists although it shouldn't" : "Event TWO doesn't exist") << std::endl;

	activity.dispatchEvent(Event("ONE"));
	activity.dispatchEvent(Event("TWO"));

	activity.addEventListener("THREE", EventListenerT<Observer>(observer, &Observer::handleEventTwo));

}


int main() {
	std::cout << "!!!Hello World!!!" << std::endl; // prints !!!Hello World!!!
	testEventDispatcher();

	Simulator simulator;

	simulator.step();


	return 0;
}


